﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int lowerBound = (int)lower.Value;//上下限的范围为1到65535整数
            int upperBound = (int)upper.Value;
            if(lowerBound<upperBound)
            {
                string message = "";
                List<int> primes = GetPrimes(lowerBound, upperBound);
                for (int i = 0; i < primes.Count; i++)
                {
                    if(i>=200){break;}//打断循环防止数据过多
                    if (i % 10 == 0)
                    {
                        message += "\n";
                    }
                    message += primes[i] + ",";
                    
                }

                MessageBox.Show(message);
            }
            else
            {
                MessageBox.Show("非法数据");//防止数据溢出，或错误
            }
        }
        
        public static List<int> GetPrimes(int lowerBound, int upperBound)
        {
            List<int> primes = new List<int>();
            for (int i = lowerBound; i <= upperBound; i++)
            {
                if (IsPrime(i))
                {
                    primes.Add(i);
                }
            }
            return primes;
        }
        
        private static bool IsPrime(int n)
        {
            if (n < 2) return false;
            for (int i = 2; i <= Math.Sqrt(n); i++)
            {
                if (n % i == 0)
                {
                    return false;
                }
            }
            return true;
        }
    }
}